import { Component, OnInit } from '@angular/core';
import { HttpService } from '../../service/http.service';
import { HttpRouterService } from '../../service/http-router.service';
import { StorageService } from '../../service/storage.service';
import { Router, ActivatedRoute } from '@angular/router';
import _ from 'underscore';
import { User } from '../user.model';
declare var mui: any;

@Component({
  selector: 'app-order-details',
  templateUrl: './order-details.component.html',
  styleUrls: ['./order-details.component.less']
})
export class OrderDetailsComponent implements OnInit {

  // public danDetail = {
  //   realname: '',
  //   money: '',
  //   mobile: '',

  //   age: '',
  //   region_name: '',

  //   occupation: '',
  //   workinglife: '',
  //   socialsecurity: '',
  //   accumulationfund: '',
  //   payrolltype: '',
  //   monthlyincome: '',

  //   insurance: '',
  //   linecard: '',
  //   housetype: '',
  //   cartype: ''
  // };
  public detail = new User();

  public isQiang = true;
  public isTao = false;

  public createOrderParam = '';
  public isbuy: Boolean;
  public isSetup = false;
  public oId = "";

  public followtime = "";
  public followrecord = "";

  constructor(
    private _http: HttpService,
    private _storage: StorageService,
    private _activatedRoute: ActivatedRoute,
    private _httpRouter: HttpRouterService,
    private _router: Router
  ) { }

  ngOnInit() {
    this.oId = this._storage.getSession('oId');
    this._activatedRoute.queryParams.subscribe(params => {
      this.isQiang = params.isQiang === 'true' ? true : false;
      this.isTao = params.isTao === 'true' ? true : false;
      this.getDetails();
    });


  }
  getDetails(){
    const param = 'id=' + this._storage.getSession('danId');
    this._http.post(this._httpRouter.userDetail, param).then(res => {
      if (res.code === 1) {
        this.detail = res.data;
        if (this.isQiang) {
          if (this.detail['mobile']) {
            this.detail['mobile'] = this.detail['mobile'].substr(0, 3) + '****' + this.detail['mobile'].substr(7);
          }
          if (this.detail['idcard']) {
            this.detail['idcard'] = this.detail['idcard'].substr(0, 2) + '****************';
          }
          if (this.detail['buy'] === 'yes') {
            this.isbuy = true;
          }else {
            this.isbuy = false;
          }
        }
        this.detail['age'] = this.age(this.detail['birthday']);
        this.detail['createtime'] = this.timeAgo(this.detail['createtime']);
        this.detail['customer_price'] = this.isTao ? 0 : this.detail['customer_price'];
        console.log(this.detail);
      }
    });
  }

  tips(danId, danMoney, $event) {
    $event.preventDefault();
    $event.stopPropagation();
    const btnArray = ['否', '是'];
    const that = this;
    mui.confirm('您确认要抢这个单子吗？', '抢单', btnArray, function (e) {
      if (e.index === 1) {
        that.createOrderParam = 'token=' + that._storage.getLocal('token') +
          '&customer_id=' + danId +
          '&order_money=' + danMoney;
        that._http.post(that._httpRouter.createorder, that.createOrderParam).then(res => {
          if (res.code === 1) {
            this.yiQaing = true;
          }
          alert(res.msg);
        });
      } else {
        return;
      }
    });
  }

  age(str) {
    const birList = [parseInt(str.substr(0, 4), 0), parseInt(str.substr(5, 7), 0), parseInt(str.substr(8, 10), 0)];
    const now = new Date();
    const newList = [now.getFullYear(), now.getMonth() + 1, now.getDate()];
    if (birList[0] < newList[0]) {
      if (birList[1] < newList[1]) {
        return newList[0] - birList[0];
      } else if (birList[1] === newList[1]) {
        if (birList[2] <= newList[2]) {
          return newList[0] - birList[0];
        } else {
          return newList[0] - birList[0] - 1;
        }
      } else {
        return newList[0] - birList[0] - 1;
      }
    } else {
      return 0;
    }
  }
  opensetup(){
    this.isSetup = true;
  }
  closedsetup(){
    this.isSetup = false
  }
  setup(id){
    let token = this._storage.getSession('token');
    const param = 'followtime=' + this.followtime
      + "&followrecord="+ this.followrecord
      +'&oid=' + this.oId
      +'&token=' + token;
    this._http.post(this._httpRouter.orderremark, param).then(res => {
      this.isSetup = false;
    });
  }

  timeAgo(time) {
    const date = new Date(time);
    return this.getDateDiff(date.getTime());
  }

  getDateDiff(dateTimeStamp) {
    const minute = 1000 * 60;
    const hour = minute * 60;
    const day = hour * 24;
    const halfamonth = day * 15;
    const month = day * 30;
    const now = new Date().getTime();
    const diffValue = now - dateTimeStamp;
    if (diffValue < 0) {return; }
    const monthC = diffValue / month;
    const weekC = diffValue / (7 * day);
    const dayC = diffValue / day;
    const hourC = diffValue / hour;
    const minC = diffValue / minute;
    let result: any;
    if (monthC >= 1) {
      result = '' + parseInt(monthC.toString(), 0) + '月前';
    }else if (weekC >= 1) {
      result = '' + parseInt(weekC.toString(), 0) + '周前';
    }else if (dayC >= 1) {
      result = '' + parseInt(dayC.toString(), 0) + '天前';
    }else if (hourC >= 1) {
      result = '' + parseInt(hourC.toString(), 0) + '小时前';
    }else if (minC >= 1) {
      result = '' + parseInt(minC.toString(), 0) + '分钟前';
    }else {
      result = '刚刚';
    }
    return result;
  }

}
